<template>
  <div class="container">
    <div class="main">
      <el-card>
        <el-row>
          <el-col :span="20">
            <el-form
              :inline="true"
              :model="formInline"
              class="demo-form-inline"
            >
              <el-form-item label="关键字">
                <el-input v-model="formInline.keyword"></el-input>
              </el-form-item>
              <el-form-item label="状态">
                <el-select v-model="formInline.state" placeholder="请选择">
                  <el-option label="已启用" value="1"></el-option>
                  <el-option label="已禁用" value="0"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button type="" @click="clear()">清除</el-button>
                <el-button type="primary" @click="filter()">搜索</el-button>
              </el-form-item>
            </el-form>
          </el-col>
          <el-col :span="4">
            <el-button type="success" icon="el-icon-edit" size="small"  @click="hanldePreview()"
              >新增技巧</el-button
            >
          </el-col>
        </el-row>
        <!-- 消息提示框 -->
        <el-alert :title="`数据一共 ${ total } 条`" type="info" show-icon :closable="false">

        </el-alert>
        <!-- 列表 -->
        <el-table :data="data" highlight-current-row>
          <el-table-column
            label="序号"
            type="index"
            :index="indexMethod"
            width="80"
          >
          </el-table-column>
          <el-table-column prop="title" label="文章标题" width="500">
             <template slot-scope="scope">
            {{scope.row.title}}
            <a @click.prevent="openVideo(scope.row.videoURL)" v-if="scope.row.videoURL" href="#" class="el-icon-film"></a>
          </template>
          </el-table-column>
          <el-table-column prop="visits" label="阅读数" width="150">
          </el-table-column>
          <el-table-column prop="username" label="录入人" width="150">
          </el-table-column>
          <el-table-column  label="录入时间">
            <template #default={row}>{{row.createTime|parseTimeByString}}</template>
          </el-table-column>
          <el-table-column label="状态" width="150">
            <template #default="{ row }">
              {{ row.state ? "已启用" : "已禁用" }}
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-link
                :underline="false"
                type="primary"
                class="width"
                @click="handlePre(scope.row)"
                >预览</el-link
              >
              <el-link
                :underline="false"
                type="primary"
                class="width"
                @click="handleChangeClick(scope.row)"
                >{{ scope.row.state ? "禁用" : "启用" }}</el-link
              >
              <el-link
                :disabled="handleChangeDisabled(scope.row)"
                :underline="false"
                type="primary"
                class="width"
                @click="hanldePreview(scope.row)"
                >修改</el-link
              >
              <el-link
                :disabled="handleChangeDisabled(scope.row)"
                :underline="false"
                type="primary"
                class="width"
                @click="del(scope.row)"
                >删除</el-link
              >
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination
          background
          layout="prev, pager, next, sizes, jumper"
          :total="total"
          :page-size="formInline.pagesize"
          :current-page="formInline.page"
          @current-change="pager"
          :page-sizes="[1,2,3,4,8,10]"
          @size-change="handleSizeChange"
        ></el-pagination>
      </el-card>
      <!-- 添加 修改对话框 -->
      <Articles-add :data="currArticle" :dialogVisible.sync="dialogVisible"  ref="hanldeInfo" @updateList="updateList()"></Articles-add>
      <!-- 预览对话框 -->
      <articles-preview :data='currArticle' :dialogPre.sync="dialogPre"></articles-preview>
      <!-- 视频 -->
     <div class="videoa" v-if="videoURL">
      <div class="close" @click="closeVideo()"><span class="el-icon-close"></span></div>
      <div class="box">
        <video ref="video" :src="videoURL"></video>
      </div>
    </div>
  </div>
  </div>
</template>

<script>
import { list, remove } from '@/api/hmmm/articles.js'
import ArticlesAdd from '@/module-hmmm/components/articles-add'
import ArticlesPreview from '@/module-hmmm/components/articles-preview'
export default {
  name: 'Articles',
  components: {
    ArticlesAdd,
    ArticlesPreview
  },
  data () {
    return {
      dialogPre: false,
      dialogVisible: false,
      formInline: {
        keyword: null,
        state: null,
        page: 1,
        pagesize: 10
      },
      currArticle: {},
      data: [],
      currentRow: null,
      total: 0,
      videoURL: null
    }
  },
  created () {
    // 获取文章列表
    this.getArticlesList()
  },
  methods: {
    openVideo (url) {
      this.videoURL = url
      this.$nextTick(() => {
        this.$refs.video.play()
      })
    },
    // 视频
    closeVideo () {
      this.$refs.video.pause()
      this.videoURL = null
    },
    // 预览
    handlePre (art) {
      this.currArticle = art
      this.dialogPre = true
    },
    // 进行筛选
    filter () {
      this.formInline.page = 1
      this.getArticlesList()
    },
    // 清除筛选条件
    clear () {
      this.formInline = {
        keyword: null,
        state: null,
        page: 1,
        pagesize: 10
      }
    },
    // 分页
    pager (num) {
      // console.log(num, 555)
      this.formInline.page = num

      this.getArticlesList()
    },
    handleSizeChange (size) {
      // 切换条数
      this.formInline.pagesize = size
      this.formInline.page = 1
      this.getArticlesList()
    },
    //  修改功能
    hanldePreview (art = {}) {
      this.dialogVisible = true
      this.currArticle = art
      console.log(art, 9888)
      console.log(this.currArticle, 8555)
      this.$nextTick(() => {
        this.$refs.hanldeInfo.open()
      })
    },
    updateList () {
      this.getArticlesList()
    },

    handleChangeDisabled (row) {
      return !row.state
    },
    handleChangeClick (row) {
      // console.log(row, 333)
      row.state = row.state === 1 ? +0 : +1
    },
    // 删除
    async del (id) {
      // console.log(id)
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await remove(id)
        console.log(res)
        if (this.data.length === 1 && this.formInline.page > 1) {
          this.formInline.page--
        }
        this.getArticlesList()
      })
    },
    // 获取文章列表
    async getArticlesList () {
      // console.log(this.id)
      const {
        data
      } = await list(this.formInline)
      console.log(data, 111)
      this.data = data.items
      this.total = data.counts
    },
    indexMethod (index) {
      // console.log(index, 11111)
      const num = index + 1 + (this.formInline.page - 1) * this.formInline.pagesize
      // console.log(num, 222)
      return num
    }
  }
}
</script>

<style scoped lang='scss'>
.main {
  margin: 15px;
}
.width {
  margin: 0 5px;
}
// .color {
//   width: 100%;
//   background-color: #f8f8f8;
//   margin: 10px 0;
// }
.videoa{

  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,.3);
  overflow: hidden;
  z-index: 9999;
  .close{
    width: 50px;
    height: 50px;
    position: absolute;
    top: 30px;
    left: 50%;
    transform: translate(-50%,0);
    background: rgba(0,0,0,0.4);
    box-shadow: 0 0 5px rgba(0,0,0,0.4);
    border-radius: 50%;
    text-align: center;
    line-height: 50px;
    color: #fff;
    font-size: 20px;
  }
  .box{
    width: 800px;
    height: 600px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    video{
      width: 100%;
      height: 100%;
    }
  }

}
</style>
